﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Diagnostics;
using System.Web.Services.Protocols;

namespace BadHabit.Factory.Views {

    /// <summary>
    /// Static class with method to show a error message.
    /// </summary>
    public partial class Error : BadHabit.WPFTools.ShapedWindow {
        
        /// <summary>
        /// Default constructor.
        /// </summary>
        /// <param name="ex"></param>
        private Error(Exception ex, string title, string detail) {
            InitializeComponent();
       
            Client.IsReadingData = false;

            this.Title = title;

            if (ex == null)
                return;
            
            // logs the error
            Helper.LogError(ex);

            // is a not connected Exception
            
            if (ex is Exceptions.NotConnectedException) {
                messageTextBlock.Text = global::BadHabit.Factory.Windows.Properties.Resources.ErrorMessage_NOT_CONNECTED;
                errorImage.Source = new BitmapImage(new Uri("pack://application:,,,/Images/errorNetwork.png"));
                return;
            }

            // is a security Exception
            if (ex is System.Security.SecurityException) {
                messageTextBlock.Text = global::BadHabit.Factory.Windows.Properties.Resources.ErorMessage_SECURITY;
                errorImage.Source = new BitmapImage(new Uri("pack://application:,,,/Images/errorSecurity.png"));
                return;
            }

            //  if is a network error
            if (ex is System.Net.WebException) {
                messageTextBlock.Text = global::BadHabit.Factory.Windows.Properties.Resources.ErrorMessage_NETWORK_DOWN;
                errorImage.Source = new BitmapImage(new Uri("pack://application:,,,/Images/errorNetwork.png"));
                return;
            }

            

            // if is a soap error
            if (ex is SoapException) {

                SoapException se = (SoapException)ex;
                if (se.Code == SoapException.VersionMismatchFaultCode) {
                    messageTextBlock.Text = global::BadHabit.Factory.Windows.Properties.Resources.ErrorMessage_OLD_VERSION;
                    errorImage.Source = new BitmapImage(new Uri("pack://application:,,,/Images/errorVersion.png"));                                        
                }
                if (se.Code == SoapException.ServerFaultCode) {
                    messageTextBlock.Text = global::BadHabit.Factory.Windows.Properties.Resources.ErrorMessage_SERVER_ERROR;
                    errorImage.Source = new BitmapImage(new Uri("pack://application:,,,/Images/errorServer.png"));
                }
                if (se.Code == SoapException.ClientFaultCode) {
                    messageTextBlock.Text = global::BadHabit.Factory.Windows.Properties.Resources.ErrorMessage_INVALID_DATA;
                    errorImage.Source = new BitmapImage(new Uri("pack://application:,,,/Images/errorClient.png"));
                }
                return;
            }

            if (ex.Source.Equals("System.Web.Services")) {
                messageTextBlock.Text = global::BadHabit.Factory.Windows.Properties.Resources.ErrorMessage_SERVER_ERROR;
                errorImage.Source = new BitmapImage(new Uri("pack://application:,,,/Images/errorServer.png"));
                return;
            }

            messageTextBlock.Text = global::BadHabit.Factory.Windows.Properties.Resources.ErrorMessage_BUG;
           
        }

        /// <summary>
        /// Show a error window.
        /// </summary>
        /// <param name="ex">The exception that cause the error</param>
        /// <param name="title">The title of the window</param>
        /// <param name="detail">Error details</param>
        public static void ShowError(Exception ex, string title, string detail) {
            Error alert = new Error(ex, title, detail);            
            alert.ShowDialog();            
        }

        /// <summary>
        /// Show a error window.
        /// </summary>
        /// <param name="ex">The exception that cause the error</param>
        /// <param name="title">The title of the window</param>
        public static void ShowError(Exception ex, string title) {
            Error alert = new Error(ex, title, null);
            Client.FactoryMainWindow.IsPopUpOpened = true;
            alert.ShowDialog();
        }

        /// <summary>
        /// Shows a windows alert.
        /// </summary>
        /// <param name="message">The alert message</param>
        public static void ShowAlert(string message) {
            Client.FactoryMainWindow.IsPopUpOpened = true;
            MessageBox.Show(message, global::BadHabit.Factory.Windows.Properties.Resources.Scrum_Factory_Warning, MessageBoxButton.OK, MessageBoxImage.Warning);
            Client.FactoryMainWindow.IsPopUpOpened = false;
        }


        /// <summary>
        /// Shows a dialog box.
        /// </summary>
        /// <param name="message"></param>
        /// <param name="title"></param>
        /// <param name="buttons"></param>
        /// <returns></returns>
        public static MessageBoxResult ShowDialog(string message, string title, MessageBoxButton buttons, MessageBoxImage image, MessageBoxResult defaultResult) {
            Client.FactoryMainWindow.IsPopUpOpened = true;
            MessageBoxResult d = MessageBox.Show(message, title, buttons, image, defaultResult);
            Client.FactoryMainWindow.IsPopUpOpened = false;
            return d;
        }

        /// <summary>
        /// Just closes the window.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click(object sender, RoutedEventArgs e) {
            Close();
            Client.FactoryMainWindow.IsPopUpOpened = false;
        }

        

    }
}
